<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>任务管理</title>
    <link rel="stylesheet" th:href="@{/css/base.css}" media="all">
    <link rel="stylesheet" th:href="@{/layui/css/layui.css}" media="all">
    <script type="text/javascript" th:src="@{/layui/layui.js}"></script>
    <script type="text/javascript" th:src="@{/js/jquery-3.2.0.min.js}"></script>
    <script type="text/javascript" th:src="@{/js/base.js}"></script>
    <style>
        body{
            overflow-y: auto;
        }
    </style>
</head>
<body>
<div class="container container-index">
    <form class="layui-form layui-form-pane" id="search-form" lay-filter="search-form" action="">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">所属分区</label>
                <div class="layui-input-inline">
                    <select name="zoneId">
                        <option value="" selected>请选择</option>
                        <option th:each="item : ${zone}" th:value="${item.id}" th:text="${item.name}"></option>
                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">所属实例</label>
                <div class="layui-input-inline">
                    <select name="nodeId">
                        <option value="" selected>请选择</option>
                        <option th:each="item : ${node}" th:value="${item.id}" th:text="${item.name}"></option>
                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">状态</label>
                <div class="layui-input-inline">
                    <select name="type">
                        <option value="" selected>请选择</option>
                        <option value="1">运行中</option>
                        <option value="0">已停止</option>
                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <button type="submit" class="layui-btn layui-btn-sm layui-bg-blue" lay-submit
                        lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索
                </button>
                <button type="submit" class="layui-btn layui-btn-sm layui-btn-primary" lay-submit
                        lay-filter="data-reset-btn"><i class="layui-icon">&#xe669;</i> 重 置
                </button>
            </div>
        </div>
    </form>

    <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

    <script type="text/html" id="currentTableBar">
        {{#
        var html='<span class="toolbar_span">';
            html+='<a src="javascript:void(0)" lay-event="del">删除</a>';
            html+='<a src="javascript:void(0)" lay-event="update">编辑</a>';
            html+='<a src="javascript:void(0)" lay-event="do">执行</a>';
            if(d.status) {
            html+='<a src="javascript:void(0)" lay-event="stop">停止</a>';
            }else {
            html+='<a src="javascript:void(0)" lay-event="start">启动</a>';
            }
            return html+'</span>'
        }}
    </script>
</div>


<script type="text/javascript" th:inline="javascript">
    layui.use(['jquery', 'form', 'table', 'layer'], function () {
        const form = layui.form,
            table = layui.table,
            layer = layui.layer;

        var currTable = Asurplus.tableRender({
            table: table,
            form: form,
            url: ctxPath + 'backup-task/list',
            cols: [
                [
                    {field: '', title: '', type:'numbers', templet: function (d) {return d.LAY_TABLE_INDEX + 1;}},
                    {field: 'zoneName', title: '所属分区'},
                    {field: 'nodeName', title: '实例名称'},
                    {field: 'cron', title: 'cron表达式'},
                    {field: 'status', title: '状态', templet: function (d) {
                            if(d.status) {
                                return '<span class="badge-custom-1">运行中</span>'
                            }else {
                                return '<span class="badge-custom-4">已停止</span>'
                            }
                        }},
                    {field: 'databaseName', title: '数据库名'},
                    {field: 'tablesName', title: '数据表名'},
                    {field: 'dataType', title: '备份方式', templet: function (d) {
                            if(0 == d.dataType) {
                                return '表结构';
                            }
                            if(1 == d.dataType) {
                                return '表数据';
                            }
                            return '表结构+表数据';
                        }},
                    {field: 'isCompress', title: '是否压缩', templet: function (d) {
                            if(d.param.isCompress) {
                                return '<span class="badge-custom-1">是</span>'
                            }else {
                                return '<span class="badge-custom-4">否</span>'
                            }
                        }},
                    {field: 'isUpload', title: '是否上传', templet: function (d) {
                            if(d.param.isUpload) {
                                return '<span class="badge-custom-1">是</span>'
                            }else {
                                return '<span class="badge-custom-4">否</span>'
                            }
                        }},
                    {field: 'isDelete', title: '是否删除', templet: function (d) {
                            if(d.param.isDelete) {
                                return '<span class="badge-custom-1">是</span>'
                            }else {
                                return '<span class="badge-custom-4">否</span>'
                            }
                        }},
                    {field: 'remark', title: '备注'},
                    {field: 'createTime', minWidth: 180, title: '创建时间'},
                    {title: '操作', minWidth: 200, toolbar: '#currentTableBar', fixed: 'right'}
                ]
            ]
        });

        // 表行工具
        table.on('tool(currentTableFilter)', function (obj) {
            const data = obj.data;
            // 修改
            if(obj.event === 'update') {
                Asurplus.openLayer({
                    title: '修改',
                    content: ctxPath + 'backup-task/backup-task-update/' + data.id
                }, () => {
                    currTable.reload();
                })
                return false;
            }
            // 删除
            else if (obj.event === 'del') {
                layer.confirm('确认删除', {
                    btn: ['确定'],
                    title: '提示'
                }, function(index){
                    Asurplus.get({
                        url: ctxPath + 'backup-task/delete/' + data.id
                    }, () => {
                        layer.close(index);
                        currTable.reload();
                    });
                });
                return false;
            }
            // 启动
            else if (obj.event === 'start') {
                layer.confirm('确认启动', {
                    btn: ['确定'],
                    title: '提示'
                }, function(index){
                    Asurplus.get({
                        url: ctxPath + 'backup-task/updateStatus/' + data.id + "/true"
                    }, () => {
                        layer.close(index);
                        currTable.reload();
                    });
                });
                return false;
            }
            // 停止
            else if (obj.event === 'stop') {
                layer.confirm('确认停止', {
                    btn: ['确定'],
                    title: '提示'
                }, function(index){
                    Asurplus.get({
                        url: ctxPath + 'backup-task/updateStatus/' + data.id + "/false"
                    }, () => {
                        layer.close(index);
                        currTable.reload();
                    });
                });
                return false;
            }
            // 立即执行一次
            else if(obj.event === 'do') {
                layer.confirm('确认立即执行一次', {
                    btn: ['确定'],
                    title: '提示'
                }, function(index){
                    Asurplus.get({
                        url: ctxPath + 'backup-task/run/' + data.id
                    }, () => {
                        layer.close(index);
                        layer.msg('执行成功', {icon: 1});
                        currTable.reload();
                    });
                });
                return false;
            }
        });
    })
</script>
</body>
</html>